package com.zsc.zojbackendjudgeservice.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.zsc.zojbackendmodel.dto.question.JudgeCase;
import com.zsc.zojbackendmodel.dto.question.JudgeConfig;
import com.zsc.zojbackendmodel.dto.questionSubmit.JudgeInfo;
import com.zsc.zojbackendmodel.entity.Question;
import com.zsc.zojbackendmodel.enums.JudgeInfoMessageEnum;


import java.util.List;
import java.util.Optional;

/**
 * 默认判题策略
 */

public class JavaJudgeStrategy implements JudgeStrategy{

    @Override
    public JudgeInfo executeStrategy(JudgeContext judgeContext) {

        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        //运行后的内存
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
        //运行后的时间
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);

        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;

        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);



        if(outputList.size() != inputList.size()){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }

        for(int i = 0 ; i < judgeCaseList.size(); i++){
            //获取到预期的output
            String output = judgeCaseList.get(i).getOutput();
            if(!output.equals(outputList.get(i))){
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }

        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        //预期的时间
        Long judgeConfigTime = Optional.ofNullable(judgeConfig.getTimeLimit()).orElse(0L);
        //预期的内存
        Long judgeConfigMemory = Optional.ofNullable(judgeConfig.getMemoryLimit()).orElse(0L);
        if(time > judgeConfigTime){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        if(memory > judgeConfigMemory){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
        
    }
}
